﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using SXFramWork.Core;
using SXFramWork.Core.MongoDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SXFramWork.API.Controllers.Test
{
    /// <summary>
    /// Mongodb使用(为了方便直接在控制器写业务演示代码)
    /// </summary>
    public class MongoController : WebController
    {
        private readonly IMongoService _mongoService;
        
        public MongoController(IMongoService mongoService)
        {
            _mongoService = mongoService;
        }
        /// <summary>
        /// 插入测试数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task InsertDataMany()
        {
            var db = _mongoService.GetDb();
            var tb = db.GetCollection<BsonDocument>("Test");
            var doc = new[]
            {
                new BsonDocument{
                     { "_id",new ObjectId()},
                    { "DepartmentName","开发部"},
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "狗娃2" },{"Age",20 } },
                             new BsonDocument{ { "Name", "狗剩2" },{"Age",22 } },
                              new BsonDocument{ { "Name", "铁蛋2" },{"Age",24 } }
                        }
                    },
                    {"Sum",18 },
                      { "dim_cm", new BsonArray { 14, 21 } }


                },
                 new BsonDocument{
                      { "_id",new ObjectId()},
                    { "DepartmentName","测试部"},
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "张三2" },{"Age",11 } },
                             new BsonDocument{ { "Name", "李四2" },{"Age",34 } },
                              new BsonDocument{ { "Name", "王五2" },{"Age",33 } }
                        }
                    }
                     ,
                     { "Sum",4 }
                     ,
                       { "dim_cm", new BsonArray { 14, 21 } }

                },
                  new BsonDocument{
                      { "_id",new ObjectId()},
                    { "DepartmentName","运维部"},
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "闫" },{"Age",20 } },
                             new BsonDocument{ { "Name", "王" },{"Age",22 } },
                              new BsonDocument{ { "Name", "赵" },{"Age",24 } }
                        }
                    },
                     { "Sum",2 },
                       { "dim_cm", new BsonArray { 22.85, 30 } }

                }
            };
            await tb.InsertManyAsync(doc);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IApiResult> Query()
        {
            var db = _mongoService.GetDb();
            var tb = db.GetCollection<BsonDocument>("Test");
            //根据条件筛选
            var builder = Builders<BsonDocument>.Filter;
            var filter = builder.Gt("Sum", 2);
            var list = await tb.FindAsync(filter);
            List<BsonValue> res = new List<BsonValue>();
            foreach (var item in list.ToList())
            {
                //取出整条值
                res.Add(item.AsBsonValue);
            }
            Console.WriteLine(res);
            return ApiResult.Ok(res.ToJson());
        }

    }
}
